"""
Case Type   : cm
Case Name   : cm_ctl switch切换基本参数验证
Create At   : 2024-07
Owner       : haomeng
Description :
    1、查看集群状态
    2、cm_ctl switch执行ddb模式切换
    3、cm_ctl switch commit提交
    4、cm_ctl swtitch rollback回滚
Expect      :
    1、集群所有节点状态正常，显示Normal
    2、合理报错
    3、操作成功
    4、操作成功
History     :
"""

import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger

Primary_SH = CommonSH('PrimaryDbUser')


@unittest.skipIf(3 != Primary_SH.get_node_num(), '需1主2备环境，否则不执行')
class CmCtl(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.comsh = CommonSH('PrimaryDbUser')
        self.constant = Constant()

    def test_cm(self):
        text = '----step1:查看集群状态; expect:集群状态正常,DN主在节点1----'
        self.log.info(text)

        status = self.comsh.exec_cm_ctl('query', '-Cv')
        self.log.info(status)
        self.assertIn("cluster_state   : Normal", status)

        text = '----step2:cm_ctl switch执行ddb模式切换; expect:合理报错----'
        self.log.info(text)

        switch1 = self.comsh.exec_cm_ctl('switch', '--ddb_type=ddb')
        self.log.info(switch1)
        self.assertIn('switch failed', switch1, '切换成功')

        text = '----step3:cm_ctl switch commit提交  expect:操作成功----'
        self.log.info(text)

        switch2 = self.comsh.exec_cm_ctl('switch', '--commit')
        self.log.info(switch2)
        self.assertIn('commit success', switch2, '切换失败')

        text = '----step4:cm_ctl switch rollback回滚  expect:操作成功----'
        self.log.info(text)

        switch3 = self.comsh.exec_cm_ctl('switch', '--rollback')
        self.log.info(switch3)
        self.assertIn('rollback success', switch3, '切换失败')

    def tearDown(self):
        self.log.info('-----No need to clean-----')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
